package com.pets.controller;


import com.pets.entity.FileInformation;
import com.pets.entity.Users;
import com.pets.service.IBountyListService;
import com.pets.service.IFileInformationService;
import com.pets.utils.Result;
import com.pets.viewModels.BountyListViewModels;
import com.pets.viewModels.FileInformationModels;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 文件信息 前端控制器
 * </p>
 *
 * @author 啦啦啦
 * @since 2024-04-09
 */
@RestController
@RequestMapping("/file-information")
@Api(tags = "文件信息管理接口")
public class FileInformationController {

    @Autowired
    private IFileInformationService fileInformationService;
    @Autowired
    private IBountyListService bountyListService;

    /**
     * 创建文件
     */
    @ApiOperation("创建文件（userId，文件名称必须填写）")
    @PostMapping
    public Result register(@RequestBody FileInformationModels fileInformationModels, HttpSession session) {
        //对象映射器
        ModelMapper modelMapper = new ModelMapper();
        FileInformation fileInformation = modelMapper.map(fileInformationModels, FileInformation.class);
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername()+"/"+user.getUserId());
            fileInformation.setUserId(user.getUserId());
            //完成悬赏
            if (fileInformationModels.getBountyListId()!=null){
                bountyListService.complete(fileInformationModels.getBountyListId(),user.getUserId());
            }
        }else {
            return Result.fail("未登录！");
        }
        if(fileInformation.getFileAddress()==null||fileInformation.getFileAddress().isEmpty()){
            return Result.fail("未获取到文件地址！");
        }*/
        /*//判断是否属于完成悬赏上传
        if(fileInformationModels.getBountyListId()!=null){
            Result complete = bountyListService.complete(fileInformationModels.getBountyListId(), fileInformationModels.getUserId());
            if(complete.getCode() == -1){
                return complete;
            }
        }*/
        return fileInformationService.register(fileInformation);
    }

    @ApiOperation("根据id删除文件")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return fileInformationService.deleteById(id);
    }

    @ApiOperation("修改文件信息（文件实体，不能修改userid和创建时间）")
    @PutMapping("/update")
    public Result update(@RequestBody FileInformation fileInformation,HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            if(!user.getIsAdministrators()&&!fileInformation.getUserId().equals(user.getUserId())){
                return Result.fail("不能修改他人文件！");
            }
            if(fileInformation.getFileAddress()==null||fileInformation.getFileAddress().isEmpty()){
                return Result.fail("文件地址为空！");
            }
            System.out.println(user.getUsername()+"/"+user.getUserId());
            return fileInformationService.updateData(fileInformation);
        }else {
            return Result.fail("未登录！");
        }*/
        return fileInformationService.updateData(fileInformation);
    }

    @ApiOperation("悬赏文件列表文件点赞")
    @GetMapping("/putlike")
    public Result putlike(
                       @ApiParam(value = "文件id")@RequestParam(required = false) Integer fileId,
                       @ApiParam(value = "是否点赞（传fales为取消点赞，tuer为点赞）")@RequestParam(required = false) boolean isIncrease,
                       @ApiParam(value = "用户id（当前登陆人id）")@RequestParam(required = false) Integer userId) {
        return fileInformationService.putlike(fileId,isIncrease,userId);
    }

    @ApiOperation("采纳当前文件（只穿传递被采纳文件fileId值）")
    @PutMapping("/complete")
    public Result complete(@RequestBody FileInformation fileInformation) {
        return bountyListService.complete(fileInformation.getFileId());
    }

    @ApiOperation("分页查询文件信息")
    @GetMapping("/listPage")
    public Result listPage(@ApiParam(value = "当前页码", required = true)@RequestParam(defaultValue = "1") Integer page,
                           @ApiParam(value = "每页显示数量", required = true)@RequestParam(defaultValue = "10") Integer pageSize,
                           @ApiParam(value = "文件名称搜索")@RequestParam(required = false) String fileName,
                           @ApiParam(value = "语言类型id")@RequestParam(required = false) Integer languageTypeId,
                           @ApiParam(value = "开发工具（枚举）")@RequestParam(required = false) Integer developmentTool,
                           @ApiParam(value = "项目类型（枚举）")@RequestParam(required = false) Integer projectTypesOf,
                           @ApiParam(value = "文件是否公开")@RequestParam(required = false) Boolean isOpen,
                           @ApiParam(value = "用户id（查看我的文件时传递）")@RequestParam(required = false) Integer userId) {
        return fileInformationService.selectPage(page, pageSize, fileName, languageTypeId, developmentTool,projectTypesOf,isOpen,userId);
    }



    @ApiOperation("分页悬赏下上传的文件")
    @GetMapping("/bountyPage")
    public Result bountyPage(@ApiParam(value = "当前页码", required = true)@RequestParam(defaultValue = "1") Integer page,
                             @ApiParam(value = "每页显示数量", required = true)@RequestParam(defaultValue = "10") Integer pageSize,
                             @ApiParam(value = "当前登陆人userId")@RequestParam(required = true) Integer userId,
                             @ApiParam(value = "悬赏id")@RequestParam(required = true) Integer bountyListId){
        return fileInformationService.bountyPage(page, pageSize, bountyListId,userId);
    }


    @ApiOperation("数据统计图（只开给管理员）")
    @GetMapping("/orderStatistics")
    public Result orderStatistics(
            @ApiParam(value = "开始时间,格式: yyyy-MM-dd", required = true)
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTime,
            @ApiParam(value = "结束时间,格式: yyyy-MM-dd", required = true)
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        return fileInformationService.orderStatistics(beginTime, endTime);
    }


}
